package _2022.day._003_676_实现一个魔法字典;

/**
 * @author： YHSimon
 * @date： 2022-07-11 22:23
 */
public class MagicDictionary {
    private String[] dictionary;
    public MagicDictionary() {

    }

    public void buildDict(String[] dictionary) {
        this.dictionary=dictionary;
    }

    public boolean search(String searchWord) {
        boolean flag;
        for(int i=0;i<this.dictionary.length;i++){
            String str=dictionary[i];
            if(str.length()==searchWord.length()&&!str.equals(searchWord)){
                flag=check(str, searchWord);
                if(flag){
                    return true;
                }
            }
        }
        return false;
    }

    private boolean check(String a,String b){
        int diff=0;
        for(int i=0;i<a.length();i++){
            if(a.charAt(i)!=b.charAt(i)){
                diff++;
            }
        }
        return diff==1;
    }
}
